<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8" />
<title>元模型定义</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<link href="${mvcPath}/dacp-view/aijs/css/ai.css" type="text/css" rel="stylesheet" />
<link rel="stylesheet" href="${mvcPath}/dacp-lib/ztree/zTreeStyle.css" type="text/css">
<style>
.media, .media-body {
	overflow: inherit;
	zoom: 1;
}

.list-group-item.active .dropdown-menu a {
	color: black;
}

small {
	font-size: 9px;
}
.fuelux .wizard {
	border-right: 0px;
	border-bottom: 1px solid #eee;
	box-shadow: 0 0px 0px;
	background-color: transparent;
	box-shadow: none;
	border-radius: 0px;
}

.wrapper {
	padding: 1px 15px 15px 15px;
}

.panel-body {
	padding: 2px 0px 5px 0px;
}
</style>
<script type="text/javascript" src="${mvcPath}/dacp-lib/jquery/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="${mvcPath}/dacp-lib/ztree/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript" src="${mvcPath}/dacp-lib/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="${mvcPath}/dacp-lib/underscore/underscore-min.js"></script>
<script src="${mvcPath}/dacp-lib/cryptojs/aes.js" type="text/javascript"></script>
<script src="${mvcPath}/crypto/crypto-context.js" type="text/javascript"></script>
<script src="${mvcPath}/dacp-view/aijs/js/ai.core.js"></script>
<script src="${mvcPath}/dacp-view/aijs/js/ai.field.js"></script>
<script src="${mvcPath}/dacp-view/aijs/js/ai.jsonstore.js"></script>
<script src="${mvcPath}/dacp-view/aijs/js/ai.grid.js"></script>
<script src="${mvcPath}/dacp-view/aijs/js/ai.treeview.js"> </script>
<script src="${mvcPath}/dacp-res/catalog/common/js/ai.meta.widget.js"></script>

<style>
.media, .media-body {
	overflow: inherit;
	zoom: 1;
}

.list-group-item.active .dropdown-menu a {
	color: black;
}

small {
	font-size: 9px;
}
 
.wrapper {
	padding: 1px 15px 15px 15px;
}
 
a.plan {
	font-size: 14px;
}

a.task {
	font-size: 13px;
}

.toolbar {
	margin-top: 10px;
	margin-bottom: 10px;
}

.toolbar .form-control {
	display: inline;
	padding: 1px 2px;
}

.toolbar button {
	margin-left: 10px;
}
.panel .table-striped > thead th {
    background-color: #36B0C8;
}
td{
	word-break:break-all;
}
.form-horizontal .form-group {
    margin-left: 0px;
    margin-right: 0px;
}
.ztree .line {
	height: auto;
}
.panel-footer {
	height: 25px;
}
.panel-footer .btn-xs {
	padding:0px 5px;
	margin-top:-17px;
}
</style>

 
<script>
	var modelListTreeObj;
	var objAttrTreeGridObj;
	var modelMgrTreeObj;

	var modelTreeStore =  new AI.JsonStore({
		sql: "select objcode id, case when parentcode='' or parentcode is null then 'root' else parentcode end pId, objname name from metaobjmodel order by objcode",
		table: "METAOBJMODEL",
		key: 'ID',
		pageSize: -1
	});

	var modelListStore = new AI.JsonStore({
		sql: "select * from metaobjmodel",
		table: "METAOBJMODEL",
		key: 'OBJCODE',
		pageSize: -1
	});

	var  attrSql = {
		content: "select * from metaobjcfg where 1=1 {condi} order by attrgroup",
		getContent:function(condition) {
    	  return this.content.replace('{condi}', condition || "");
    	}
	};
	var objAttrStore = new AI.JsonStore({
		sql: attrSql.getContent(),
		table: "METAOBJCFG",
		key:'XMLID',
		pageSize : -1
	});

	var defSql = {
		content: "select OBJTYPE, ATTRCODE, PARACODE, PARENTCODE, PARANAME, PARATYPE, PARATEXT, RUNDB, ORDSEQ from metaobjmodel_def where 1=1 {condi} order by ORDSEQ, ATTRCODE",
		getContent:function(condition) {
    	  return this.content.replace('{condi}', condition || "");
    	}
	};
	var modelDefStore = new AI.JsonStore({
		sql: defSql.getContent(),
		table: "METAOBJMODEL_DEF",
		key: 'OBJTYPE,ATTRCODE',
		pageSize: -1
	});

	var addNewModelItems = [
		{type:'textfield',label:'对象编号',fieldName:'OBJCODE',id:'OBJCODE', notNull : 'N'},
		{type:'textfield',label:'对象类型',fieldName:'OBJTYPE',id:'OBJTYPE', notNull : 'N'},
		{type:'textfield',label:'中文名称',fieldName:'OBJNAME',id:'OBJNAME', notNull : 'N'},
		{type:'textfield',label:'父类型编号',fieldName:'PARENTCODE',id:'PARENTCODE'},
		{type:'textfield',label:'存储表名',fieldName:'TABNAME',id:'TABNAME'},
		{type:'textfield',label:'主键字段',fieldName:'KEYFIELD',id:'KEYFIELD'},
		{type:'textfield',label:'名称字段',fieldName:'NAMEFIELD',id:'NAMEFIELD'},
		{type:'textfield',label:'日志表',fieldName:'LOGTAB',id:'LOGTAB'},
		{type:'textfield',label:'时间字段名',fieldName:'TIMEFIELD',id:'TIMEFIELD'},
		{type:'textfield',label:'图标',fieldName:'OBJICON',id:'OBJICON'},
		{type:'textfield',label:'开发地址',fieldName:'DETAILURL',id:'DETAILURL'},
		{type:'textfield',label:'运行地址',fieldName:'RUNURL',id:'RUNURL'},
		{type:'combox',label:'是否启用',fieldName:'STATE',id:'STATE',editable:'N',storesql:"Y,是|N,否"},
		{type:'textfield',label:'开发检查',fieldName:'DEVCK',id:'DEVCK'}
	];

	var getFieldCNName = function(items, fieldname) {
		for(var i in items) {
			if(items[i].fieldName == fieldname) {
				return items[i].label;
			}
		}
	};

	var addNewModel = function(parentcode) {
		var afterAdd = function(fieldVals) {
			var r = modelTreeStore.getNewRecord();
			var notNullList = 'OBJCODE,OBJTYPE,OBJNAME';
			for(var fieldName in fieldVals) {
				if(notNullList.indexOf(fieldName) != -1) {
					if(!fieldVals[fieldName]) {
						alert(getFieldCNName(addNewModelItems,fieldName) + "不能为空！");
						return false;
					}
				}
				r.set(fieldName, fieldVals[fieldName]);
			}

			var newId = r.get("OBJCODE");
			if(modelTreeStore.getRecordByKey(newId)) {
				alert("新的元模型编号与已有重复，请重新输入！");
				return false;
			}

			modelTreeStore.add(r);
			modelTreeStore.commit();
			modelTreeStore.select();
			modelListStore.select();
		};
		ai.openFormDialog('新增元模型类型', addNewModelItems, afterAdd);
		if(parentcode) {
			$("#_global_dlg_form #OBJCODE").val(parentcode + "-");
			$("#_global_dlg_form #PARENTCODE").val(parentcode);
		}
	};

	var deleteModel = function() {
		var selected = modelListTreeObj.getSelectedNodes();
		if(selected.length == 0) {
			alert("请选择要删除的节点!");
			return;
		}
		for(var i in selected) {
			var node = selected[i];
			if(node.isParent) {
				alert('该节点为父节点，请先删除所有的子节点！');
				return;
			}
			if(confirm("确认删除元模型：" + node.name + " ？")) {
				ai.executeSQL("delete from metaobjmodel where objcode = '" + node.id + "'");
			}
		}
		modelTreeStore.select();
		modelListStore.select();
		buildBaseInfoGrid();
	};

	//Panel1 -- 基本信息
	var infoItems = [
		{'name': '对象编号', 'id': 'OBJCODE'},
		{'name': '对象类型', 'id': 'OBJTYPE'},
		{'name': '中文名称', 'id': 'OBJNAME'},
		{'name': '父类型编号', 'id': 'PARENTCODE'},
		{'name': '存储表名', 'id': 'TABNAME'},
		{'name': '主键字段', 'id': 'KEYFIELD'},
		{'name': '名称字段', 'id': 'NAMEFIELD'},
		{'name': '日志表', 'id': 'LOGTAB'},
		{'name': '时间字段名', 'id': 'TIMEFIELD'},
		{'name': '图标', 'id': 'OBJICON'},
		{'name': '开发地址', 'id': 'DETAILURL'},
		{'name': '运行地址', 'id': 'RUNURL'},
		{'name': '是否启用', 'id': 'STATE'},
		{'name': '开发检查', 'id': 'DEVCK'}
	];

	var buildBaseInfoGrid = function() {
		var htmlTpl = '<tr"><td style="width: 20%">{-LABLE-}</td><td><input id="{-ID-}" style="width: 90%"></td></tr>';
		var rec = modelListStore.curRecord;
		if(!rec) {
			rec = modelListStore.getRecordByKey("META");
		}

		$("#baseInfoGrid").empty();
		for(var i in infoItems) {
			var html = htmlTpl.replace('{-LABLE-}', infoItems[i]['name']).replace('{-ID-}', infoItems[i]['id']);
			var tr = $(html);
			tr.find('input').val(rec.get(infoItems[i]['id']));
			$("#baseInfoGrid").append(tr);
		}
		$("#baseInfoGrid").find('#OBJCODE').attr('disabled', true);
	};

	var saveBaseInfoAction = function() {
		var rec = modelListStore.curRecord;
		for(var i in infoItems) {
			var newVal = $("#baseInfoGrid").find('#' + infoItems[i]['id']).val();
			rec.set(infoItems[i]['id'], newVal);
		}
		modelListStore.commit();
		alert('保存成功！');
	};

	//Panel2 -- 对象属性
	var buildObjAttrTreeGrid = function(isEditable) {
		$('#editObjAttrInfo').html('<i class="glyphicon glyphicon-user"></i>编辑');
		var objtype = modelListStore.curRecord.get("OBJTYPE");

		var addParentRecs = function() {//添加临时父节点以显示树
			var recs = [];
			var parents = ai.getStoreData("select distinct attrgroup from metaobjcfg where objtype = '" + objtype + "' order by attrgroup");
			for(var i in parents) {
				var groupname = parents[i].ATTRGROUP;
				var rec = objAttrStore.getNewRecord();
				rec.set("OBJTYPE", objtype);
				rec.set("XMLID", groupname);
				rec.set("ATTRCNNAME", groupname);
				objAttrStore.add(rec);
				recs.push(rec);
			}
			return recs;
		};

		var delParentRecs = function(recs) {
			for(var r in recs) {
				objAttrStore.remove(recs[r]);
			}
		};

		// var proValRender = function(row, col) {
		// 	var val = row[col.dataField] || "";
		// 	return '<input field="'+col.dataField+'" value="'+val+'" />';
		// };

		objAttrStore.select(attrSql.getContent("and OBJTYPE = '" + objtype + "'"));
		var arr = addParentRecs();

		var config = {
			id: "objAttrInfo",
			width: "100%",
			renderTo: "objAttr",
			headerAlign: "left",
			headerHeight: "30",
			dataAlign: "left",
			indentation: "10",
			hoverRowBackground: "true",
			folderColumnIndex: "0",
			// itemClick: itemClickEvent,
			showcheck: isEditable,
			columns :[  
				{headerText : "属性组", dataField : "ATTRGROUP", headerAlign : "center", editable:isEditable}, 
				{headerText : "序号", dataField : "SEQ", headerAlign : "center", dataAlign : "center", editable:isEditable}, 
				{headerText : "属性名称", dataField : "ATTRNAME", headerAlign : "center", dataAlign : "center", editable:isEditable}, 
				{headerText : "中文名称", dataField : "ATTRCNNAME", headerAlign : "center", dataAlign : "center", editable:isEditable}, 
				{headerText : "输入类型", dataField : "INPUTTYPE", headerAlign : "center", dataAlign : "center", editable:isEditable}, 
				{headerText : "输入参数", dataField : "INPUTPARA", width: "300", headerAlign : "center", dataAlign : "center", editable:isEditable}, 
				{headerText : "允许为空", dataField : "ISNULL", headerAlign : "center", dataAlign : "center", editable:isEditable}, 
				{headerText : "备注", dataField : "REMARK", headerAlign : "center", dataAlign : "center", editable:isEditable}, 
				{headerText : "依赖", dataField : "DEPENDENCIES", headerAlign : "center", dataAlign : "center", editable:isEditable}, 
				{headerText : "依赖条件", dataField : "CHECKITEMS", headerAlign : "center", dataAlign : "center", editable:isEditable}
			],
			keyfield: "XMLID",
			pkeyfield: "ATTRGROUP",
			titlefield: "ATTRCNNAME",
			iconfield: "",
			//sql : sql,
			store: objAttrStore,
			subtype: 'treegrid'
		};

		$("#objAttr").empty();
		var treeGrid = new TreeGrid(config);
		treeGrid.show();
		delParentRecs(arr);

		$('#objAttr').find('td').unbind('click');
	    if(isEditable) {
	    	// 编辑全部时,使输入框填充td
			// $('#objAttr').find('td input[type="text"]').width("100%");
			$('#objAttr').find('td input[type="text"]').blur(function(e) {
				var xmlid = treeGrid.getSelectedItem().data.xmlid;
				var newVal = this.value;
				var changedField = this.id.replace(xmlid, "");
				// alert("更改的字段为" + changedField);
				objAttrStore.getRecordByKey(xmlid).set(changedField, newVal);
			});
		}
		objAttrTreeGridObj = treeGrid;
	};

	var addObjAttrItems = [
		{type:'textfield',label:'对象类型',fieldName:'OBJTYPE',id:'OBJTYPE', notNull : 'N'},
		{type:'combox',label:'归属组',fieldName:'ATTRGROUP',id:'ATTRGROUP',storesql:"SELECT DISTINCT ATTRGROUP FROM METAOBJCFG WHERE OBJTYPE = '" + modelListStore.curRecord.get("OBJTYPE") + "' ORDER BY ATTRGROUP", notNull : 'N'},
		{type:'textfield',label:'属性名',fieldName:'ATTRNAME',id:'ATTRNAME', notNull : 'N'},
		{type:'textfield',label:'属性中文名称',fieldName:'ATTRCNNAME',id:'ATTRCNNAME', notNull : 'N'},
		{type:'textfield',label:'序号',fieldName:'SEQ',id:'SEQ', notNull : 'N'},
		{type:'textfield',label:'输入类型',fieldName:'INPUTTYPE',id:'INPUTTYPE'},
		{type:'textarea',label:'输入参数',fieldName:'INPUTPARA',id:'INPUTPARA'},
		{type:'combox',label:'是否允许为空',fieldName:'ISNULL',id:'ISNULL',editable:'N',storesql:"Y,是|N,否"},
		{type:'textfield',label:'依赖',fieldName:'DEPENDENCIES',id:'DEPENDENCIES'},
		{type:'textfield',label:'依赖条件',fieldName:'CHECKITEMS',id:'CHECKITEMS'},
		{type:'textarea',label:'备注',fieldName:'REMARK',id:'REMARK'}
	];

	var addObjAttrAction = function() {
		var objtype = modelListStore.curRecord.get("OBJTYPE");
		var afterSave = function(fieldVals) {
			var xmlid = ai.guid();
			var notNullList = 'OBJTYPE,ATTRGROUP,ATTRNAME,ATTRCNNAME,SEQ';

			var rec = objAttrStore.getNewRecord();
			rec.set("XMLID", xmlid);

			for(var fieldName in fieldVals) {
				if(notNullList.indexOf(fieldName) != -1) {
					if(!fieldVals[fieldName]) {
						alert(getFieldCNName(addObjAttrItems,fieldName) + "不能为空！");
						return false;
					}
				}
				rec.set(fieldName, fieldVals[fieldName]);
			}
			var name_repeat = ai.getJsonStore(attrSql.content.replace("{condi}", " and attrgroup='" + rec.get('ATTRGROUP') +"' and objtype='"+rec.get('OBJTYPE') +"' and upper(attrname)=upper('"+rec.get('ATTRNAME')+"')"));
			if (name_repeat.getCount() > 0) {
				alert("该分组内的属性名已经存在，不能重复添加！");
				return false;
			}
			var cnname_repeat = ai.getJsonStore(attrSql.content.replace("{condi}", " and attrgroup='" + rec.get('ATTRGROUP') +"' and objtype='"+rec.get('OBJTYPE') +"' and attrcnname='"+rec.get('ATTRCNNAME')+"'"));
			if (cnname_repeat.getCount() > 0) {
				alert("该分组内的属性中文名已经存在，不能重复添加！");
				return false;
			}
			if(isNaN(rec.get('SEQ'))||parseInt(rec.get('SEQ'))<1){
				alert("请输入0以上的数字序号！");
				return false;
			}
			var seq_repeat = ai.getJsonStore(attrSql.content.replace("{condi}", " and attrgroup='" + rec.get('ATTRGROUP') +"' and objtype='"+rec.get('OBJTYPE') +"' and seq="+rec.get('SEQ')));
			if (seq_repeat.getCount() > 0) {
				alert("该分组内的序号已经存在，不能重复添加！");
				return false;
			}
			objAttrStore.cache.save = [];
			objAttrStore.add(rec);
			objAttrStore.commit();
			buildObjAttrTreeGrid();
		};

		ai.openFormDialog('新增对象属性', addObjAttrItems, afterSave);
		$("#_global_dlg_form #OBJTYPE").val(objtype).attr('disabled', true);
	};

	var deleteObjAttrAction = function() {
		var items = objAttrTreeGridObj.getAllCheck();
		if(items.length<1){
			alert("请选择至少一条数据！");
			returrn;
		}
		if(!confirm("确认删除所选对象属性？")) {
			return;
		}
		for(var i in items) {
			if(!items[i].ATTRNAME) {
				continue;
			}
			var rec = objAttrStore.getRecordByKey(items[i].XMLID);
			objAttrStore.remove(rec);
		}
		objAttrStore.commit();
		alert("删除成功！");
		buildObjAttrTreeGrid();
	};


	//Panel3 -- 模型管理
	var modelDefFormItems = [
		{type:'combox',label:'执行数据库',fieldName:'RUNDB',id:'RUNDB',storesql:'select dbname V,cnname K from metadbcfg'},
		{type:'textfield',label:'参数英文名',fieldName:'PARACODE',id:'PARACODE', notNull : 'N'},
		{type:'textfield',label:'参数中文名',fieldName:'PARANAME',id:'PARANAME'},
		{type:'textarea',label:'参数脚本',fieldName:'PARATEXT',id:'PARATEXT'}
	];
	
	var addModelDefAction = function() {
		var selected = modelMgrTreeObj.getSelectedItem().data;
		var objtype = modelListStore.curRecord.get("OBJTYPE");
		var parentcode = selected.ATTRCODE;
		var paratype = 'para';

		var childCount = ai.getStoreData("SELECT COUNT(1) count FROM metaobjmodel_def where OBJTYPE = '" + objtype + "' AND PARENTCODE = '" + selected.ATTRCODE + "'")[0].COUNT;
		var attrcode = selected.ATTRCODE + "-" + (childCount + 1);
		var ordseq = childCount + 1;

		var afterAdd = function(fieldVals) {
			var notNullList = 'PARACODE';
			var rec = modelDefStore.getNewRecord();

			for(var fieldName in fieldVals) {
				if(notNullList.indexOf(fieldName) != -1) {
					if(!fieldVals[fieldName]) {
						alert(getFieldCNName(modelDefFormItems,fieldName) + "不能为空！");
						return false;
					}
				}
				rec.set(fieldName, fieldVals[fieldName]);
			}
			rec.set('ATTRCODE', attrcode);
			rec.set('OBJTYPE', objtype);
			rec.set('ORDSEQ', ordseq);
			rec.set('PARENTCODE', parentcode);
			rec.set('PARATYPE', paratype);

			modelDefStore.add(rec);
			modelDefStore.commit();
			buildModelMgrTree();
		};

		ai.openFormDialog('新增模型规则', modelDefFormItems, afterAdd);
	};

	var editModelDefAction = function() {
		var selected = modelMgrTreeObj.getSelectedItem().data;
		var objtype = modelListStore.curRecord.get("OBJTYPE");
		var curRec = modelDefStore.getRecordByKey(objtype + "," + selected['ATTRCODE']);

		var afterEdit = function(fieldVals) {
			var notNullList = 'PARACODE';

			for(var fieldName in fieldVals) {
				if(notNullList.indexOf(fieldName) != -1) {
					if(!fieldVals[fieldName]) {
						alert(getFieldCNName(modelDefFormItems,fieldName) + "不能为空！");
						return false;
					}
				}
				curRec.set(fieldName, fieldVals[fieldName]);
			}
			modelDefStore.commit();
			buildModelMgrTree();
		};

		ai.openFormDialog('编辑模型规则', modelDefFormItems, afterEdit);
		for(var i in modelDefFormItems) {
			var field = modelDefFormItems[i].fieldName;
			$("#_global_dlg_form #" + field).val(curRec.get(field));
		}
		$("#_global_dlg_form #ATTRCODE").attr('disabled', true);
	};

	var deleteModelDefAction = function() {
		var selected = modelMgrTreeObj.getSelectedItem().data;
		var objtype = modelListStore.curRecord.get("OBJTYPE");
		var attrcode = selected.ATTRCODE;

		if(confirm("确认删除吗？")) {
			ai.executeSQL("delete from metaobjmodel_def where OBJTYPE ='" + objtype + "' and ATTRCODE = '" + attrcode + "'");
			alert('删除成功！');
			buildModelMgrTree();
		}
	};

	var buildModelMgrTree = function(isEditable) {
		var objtype = modelListStore.curRecord.get("OBJTYPE");
		addObjAttrItems[1].storesql="SELECT DISTINCT ATTRGROUP FROM METAOBJCFG WHERE OBJTYPE = '"+objtype+ "' ORDER BY ATTRGROUP";
		modelDefStore.select(defSql.getContent("and OBJTYPE = '" + objtype + "'"));

		var actRender = function(row, col) {
			var paratype = row.PARATYPE;
			var paracode = row.PARACODE;

			if(paratype == 'para') {
				return "<a href='javascript:editModelDefAction()'><button>编辑</button></a> | <a href='javascript:deleteModelDefAction()'><button>删除</button></a>";
			} else if(paracode != '' && paratype != 'index') {
				return "<a href='javascript:editModelDefAction()'><button>编辑</button></a> | <a href='javascript:deleteModelDefAction()'><button>删除</button></a>";
			} else {
				return "<a href='javascript:addModelDefAction()'><button>增加</button></a>";
			}
		};

		var paraNameRender = function(row, col){
			var paratype = row.PARATYPE;
			var paracode = row.PARACODE;
			var paraname = row.PARANAME;

			if(paratype == 'index') {
				return "<b>" + paraname + "</b>";
			}

			var result = "";
			if(paratype == 'attrlist') {
				result = "<img src=${mvcPath}/dacp-res/me/images/portlet_resize.gif />&nbsp;&nbsp;" + paracode ;
			} else {
				result="&nbsp;&nbsp;" + paracode + "";
			};

			result = result + "(" + paraname + ")";
			return result;
	    };

		var config = {
			id: "modelMgrInfo",
			width: "100%",
			renderTo: "modelMgr",
			headerAlign: "left",
			headerHeight: "30",
			dataAlign: "left",
			indentation: "10",
			hoverRowBackground: "true",
			folderColumnIndex: "0",
			// itemClick: itemClickEvent,
			showcheck: isEditable,
			columns :[  
				{headerText : "参数中文名", dataField : "PARANAME", headerAlign : "center", width: '25%', handler: paraNameRender, editable:isEditable}, 
				{headerText : "参数内容", dataField : "PARATEXT", headerAlign : "center", dataAlign : "left", editable:isEditable}, 
				{headerText : "执行数据库", dataField : "RUNDB", headerAlign : "center", dataAlign : "center", width: '13%', editable:isEditable},
				{headerText : "操作", dataField : null, headerAlign : "center", dataAlign : "center", width: '20%', handler: actRender}
			],
			keyfield: "ATTRCODE",
			pkeyfield: "PARENTCODE",
			titlefield: "PARANAME",
			iconfield: "",
			//sql : sql,
			store: modelDefStore,
			subtype: 'treegrid'
		};

		$("#modelMgr").empty();
		var treeGrid = new TreeGrid(config);
		treeGrid.show();

		modelMgrTreeObj = treeGrid;
	};


	$(document).ready(function() {

		var setting = {
			view: {selectedMulti: false},
			check: {enable: false},
			data: {simpleData: {enable: true}},
			callback: {
				onClick: function(event, treeId, treeNode) {
					var objcode = treeNode.id;
					$("#modelcfg").attr('objcode', objcode);
					var r = modelListStore.getRecordByKey(objcode);
					if(r) {
						modelListStore.curRecord = r;
					}
					buildBaseInfoGrid();
					buildObjAttrTreeGrid();
					buildModelMgrTree();
				}
			}
		};

		var buildZTree = function(models) {
			var zNodesNew = [];
			if(models&&models.length>1){
				for(var i=0;i<models.length;i++){
					var val = models[i];
					zNodesNew.push({id:val.ID,pId:val.PID,name:val.NAME});
				}
				$("#metamodeltree").empty();
				$.fn.zTree.init($("#metamodeltree"), setting, zNodesNew);
			} else {
				$("#metamodeltree").append("找不到数据！");
			}
			modelListTreeObj = $.fn.zTree.getZTreeObj("metamodeltree");
			modelListTreeObj.expandAll(true); 
		};

		modelTreeStore.on('dataload',function(){
			buildZTree(modelTreeStore.root);
		});
 		modelTreeStore.select();

		$("#addMetamodel").click(function() {
			var parentcode = null;
			var selected = modelListTreeObj.getSelectedNodes();
			if(selected.length > 1) {
				alert('只能选择一个节点！');
				return;
			} 
			if(selected.length == 1) {
				parentcode = selected[0].id;
			}

			//打开增加节点表单
			addNewModel(parentcode);
		});

		$("#deleteMetamodel").click(function() {
			deleteModel();
		});

		$("#collectMetamodel").click(function() {

		});

		$("#saveBaseInfo").click(function() {
			saveBaseInfoAction();
		});

		$("#addObjAttrInfo").click(function() {
			addObjAttrAction();
		});

		$("#editObjAttrInfo").click(function() {
			if($('#objAttr').find('td input[type="text"]').length == 0) {
				buildObjAttrTreeGrid(true);
				$('#editObjAttrInfo').html('<i class="glyphicon glyphicon-user"></i>取消编辑');
			} else {
				buildObjAttrTreeGrid(false);
			}
		});

		$("#saveObjAttrInfo").click(function() {
			if($('#objAttr').find('td input[type="text"]').length == 0) {
				alert("请先编辑！");
				return;
			}
			var group_seq=new Array();
			var group_name=new Array();
			var group_cnname=new Array();
			for(var i=0;i<$('#objAttr').find('td input[datafield="SEQ"]').length;i++){
				if(isNaN(($('#objAttr').find('td input[datafield="SEQ"]'))[i].value)||$.trim($('#objAttr').find('td input[datafield="SEQ"]')[i].value)==""||parseInt(($('#objAttr').find('td input[datafield="SEQ"]'))[i].value)<1){
					alert("第"+(i+1)+"行请保存0以上的数字序号！")
					return;
				};
				group_seq[i]=($('#objAttr').find('td input[datafield="ATTRGROUP"]'))[i].value+"-"+($('#objAttr').find('td input[datafield="SEQ"]'))[i].value;
				group_name[i]=($('#objAttr').find('td input[datafield="ATTRGROUP"]'))[i].value+"-"+($('#objAttr').find('td input[datafield="ATTRNAME"]'))[i].value;
				group_cnname[i]=($('#objAttr').find('td input[datafield="ATTRGROUP"]'))[i].value+"-"+($('#objAttr').find('td input[datafield="ATTRCNNAME"]'))[i].value;
			}
			group_seq.sort();
			group_name.sort();
			group_cnname.sort();
			for(var i=0;i<group_seq.length-1;i++){
				if(group_seq[i]==group_seq[i+1]){
					alert((group_seq[i]).split("-")[0]+"组下的序号："+(group_seq[i]).split("-")[1]+" 重复，请检查！");
					return;
				}
				if(group_name[i]==group_name[i+1]){
					alert((group_seq[i]).split("-")[0]+"组下的属性名："+(group_name[i]).split("-")[1]+" 重复，请检查！");
					return;
				}
				if(group_cnname[i]==group_cnname[i+1]){
					alert((group_cnname[i]).split("-")[0]+"组下的中文名称："+(group_cnname[i]).split("-")[1]+" 重复，请检查！");
					return;
				}
			}
			objAttrStore.cache.save = [];
			objAttrStore.commit();
			buildObjAttrTreeGrid(false);
			alert("保存成功！");	
		});

		$("#deleteObjAttrInfo").click(function() {
			if($('#objAttr').find('td input[type="text"]').length == 0) {
				alert("请先选择编辑！");
				return;
			}
			deleteObjAttrAction();
		});

		buildBaseInfoGrid();
		buildObjAttrTreeGrid();
		buildModelMgrTree();
	});


</script>
</head>
<body class="">
	<div class="container-fluid" >
		<div class="row">
			<div class="col-sm-2" style="padding-left: 1px; padding-right: 1px; padding-top: 5px">
				<!-- <h5> &nbsp;<small id='tip'> </small> </h5> -->
				<section class="panel panel-info portlet-item" style="opacity: 1; z-index: 0;">
					<header class="panel-heading" style="height: 25px; line-height: 5px;"> 元模型列表 </header>
					<div class="panel-body">
						<div class="tab-content">
							<footer class="panel-footer"> 
								<a href="#" id="addMetamodel" class="btn btn-primary btn-xs" enabled=""><i class="glyphicon glyphicon-user"></i>增加</a>&nbsp;
								<a href="#" id="deleteMetamodel" class="btn btn-primary btn-xs" enabled=""><i class="glyphicon glyphicon-user"></i>删除</a>&nbsp;
								<!-- <a href="#" id="collectMetamodel" class="btn btn-danger btn-xs" enabled=""><i class="glyphicon glyphicon-user"></i>采集</a>&nbsp; -->
							</footer>
							<div class="panel-body slim-scroll" style="width: 100%; height: 600px; padding: 0px; overflow: auto">
								<ul id="metamodeltree" class="ztree"></ul>
							</div>
						</div>
					</div>
				</section>
			</div>
			<div class="col-sm-10" style="padding-left: 3px; padding-right: 1px; padding-top: 5px">

				<!-- <h5><span id="funaction">&nbsp;</span></h5> -->

				<section class="panel panel-info portlet-item" style="opacity: 1; z-index: 0;">
					<header class="panel-heading" style="height: 25px; line-height: 5px;">
						<span class="hidden-sm"> &nbsp; </span> <span id="taskname"
							style="float: left">元模型配置</span>
						<ul class="nav nav-tabs pull-right" >
							<li class="active" ><a href="#tab1" data-toggle="tab" style="height: 25px; line-height: 5px;"> <i class="fa fa-comments text-muted" style="line-height: 5px;"> </i> 基本信息 </a></li>
							<li class=""><a href="#tab2" data-toggle="tab" style="height: 25px; line-height: 5px;"> <i class="fa fa-user text-muted" style="line-height: 5px;"> </i> 对象属性 </a></li>
							<li class=""><a href="#tab3" data-toggle="tab" style="height: 25px; line-height: 5px;"> <i class="fa fa-user text-muted" style="line-height: 5px;"> </i> 模型管理 </a></li>
						</ul>

					</header>
					<div class="panel-body">
						<div class="tab-content">
							<div class="tab-pane active" id="tab1">
								<footer class="panel-footer"> 
									<button id="saveBaseInfo" class="btn btn-primary  btn-xs" enabled=""><i class="glyphicon glyphicon-user"></i>保存</button>
								</footer>
								<div class="panel-body slim-scroll active" style="width: 100%; height: 600px; padding: 0px; overflow: auto">
									<table class="table table-bordered table-striped" style="margin-left: 10px; margin-top: 10px; width: 90%;" id="baseInfoGrid">
											<!-- <tr"><td style="width: 20%">对象类型</td><td><input id="objtype" style="width: 90%"></td></tr> -->
									</table>
								</div>
							</div>
							<div class="tab-pane" id="tab2">
								<footer class="panel-footer" > 
									<button id="addObjAttrInfo" class="btn btn-primary  btn-xs" enabled=""><i class="glyphicon glyphicon-user"></i>新增</button>&nbsp;
									<button id="editObjAttrInfo" class="btn btn-primary  btn-xs" enabled=""><i class="glyphicon glyphicon-user"></i>编辑</button>&nbsp;
									<button id="saveObjAttrInfo" class="btn btn-primary  btn-xs" enabled=""><i class="glyphicon glyphicon-user"></i>保存</button>&nbsp;
									<button id="deleteObjAttrInfo" class="btn btn-primary  btn-xs" enabled=""><i class="glyphicon glyphicon-user"></i>删除</button>
								</footer>
								<div class="panel-body slim-scroll active" style="width: 100%; height: 600px; padding: 0px; overflow: auto">
									<div class="panel-body slim-scroll" id="objAttr" style="width: 100%; height: 600px; padding: 0px; overflow: auto">

									</div>
								</div>
							</div>
							<div class="tab-pane" id="tab3">
								<div class="panel-body slim-scroll" id="modelMgr" style="width: 100%; height: 623px; padding: 0px; overflow: auto">
									
								</div>
								<footer class="panel-footer" >
									<!-- <button id="saveBaseInfo" class="btn btn-danger  btn-xs" enabled=""><i class="glyphicon glyphicon-user"></i>保存</button> -->
								</footer>
							</div>
						</div>
					</div>
				</section>
			</div>
		</div>
	</div>
 
</body>
</html>